using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Logger;
using FiltronASP.Models.DAO.Interfaces;

namespace FiltronASP.Models.DAO.Extensions
{
    /// <summary>
    /// Klasa zawierajaca rozszerzenia metod zwiazanych z tabela stron.
    /// </summary>
    public static class PageExtensions
    {

        /// <summary>
        /// Metoda pomocnicza konwertujaca liste dostepnych grup stron z bazy do 
        /// IEnumerable<SelectListItem>.
        /// </summary>
        /// <param name="pages">kolekcja</param>
        /// <param name="selectedId">wybrane id</param>
        /// <returns>liste mozliwa do wrzucenia w drop downa</returns>
        public static IEnumerable<SelectListItem> ToSelectListItems(this IQueryable<Page> pages,
            int? selectedId)
        {
            if (pages == null)
            {
                return null;
            }   //if
            if (selectedId != null)
            {
                return pages.OrderBy(page => page.page_name)
                         .Select(page =>
                             new SelectListItem
                             {
                                 Selected = (page.page_id == selectedId),
                                 Text = page.page_name,
                                 Value = page.page_id.ToString()
                             });
            }   //if not null
            else
            {
                return pages.OrderBy(page => page.page_name)
                         .Select(page =>
                             new SelectListItem
                             {
                                 Text = page.page_name,
                                 Value = page.page_id.ToString()
                             });
            }   //else not null
        }

        /// <summary>
        /// Metoda walidujaca zgodnosc edytowanych danych strony z indeksem na wersji jezykowej, linku i aliasie.
        /// </summary>
        /// <param name="pageDAO">page dao</param>
        /// <param name="languageId">wersja jezykowa</param>
        /// <param name="pageAlias">alias</param>
        /// <param name="pageLink">link</param>
        /// <param name="pageIdToExclude">id pomijanej (biezacej) strony</param>
        /// <returns>true, gdy ok, false wpp</returns>
        public static bool IsPageAliasLinkAndLanguageUnique(this IPageDAO pageDAO, int? languageId, 
            string pageAlias, string pageLink, int? pageIdToExclude)
        {
            if (!languageId.HasValue)
            {
                Logger.MyLogger.Log("PageExtensions exception: IsPageAliasLinkAndLanguageUnique method, language id null...");
                return false;
            }   //if
            if (!pageIdToExclude.HasValue)
            {
                Logger.MyLogger.Log("PageExtensions exception: IsPageAliasLinkAndLanguageUnique method, pageIdToExclude id null...");
                return false;
            }   //if
            try
            {
                return (pageDAO.FindByLanguageAlliasLinkAndNotId(languageId.Value, pageAlias,
                    pageLink, pageIdToExclude.Value) == null);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }


    }
}
